package com.xinqi.common.base.utils.enums;

import com.xinqi.common.base.data.IdNameDTO;
import org.apache.commons.lang3.EnumUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 枚举工具类
 *
 * @author linkai
 * @date 2022/11/25
 */
public interface Enums {

    static <T extends Enum> List<IdNameDTO> enum2list(T[] e) {
        List<IdNameDTO> statusList = new ArrayList<>(e.length);
        for (Enum o : e) {
            statusList.add(new IdNameDTO().setId((long) o.ordinal()).setName(o.name()));
        }
        return statusList;
    }

    static <T extends Enum> List<IdNameDTO> enum2list(Class<T> c) {
        return enum2list(c.getEnumConstants());
    }

    static <T extends Enum<T>> T of(Class<T> c, Integer e) {
        if (e == null) {
            return null;
        }
        List<T> enumList = EnumUtils.getEnumList(c);
        if (e <= enumList.size()) {
            return enumList.get(e);
        }
        throw new IllegalArgumentException("get enum error! [enum = " + c.getSimpleName() + ", ordinal=" + e + "]");
    }

}
